<?php

namespace App\Models;

class WinningModel
{
    /**
     * 抽奖名单
     * return Array
     * */
    public static function getWinningInside($conn,$sn){
        $querySql="SELECT `username` FROM `winning` where `sn` = '$sn' and `is_winning` = 1 order by `time` asc limit 30";
        return $conn->readConn($querySql,true);
    }
    /**
     * 中奖人数和金额
     * return Array
     * */
    public static function getWinningCountInside($conn,$sn,$level){
        $querySql="SELECT COUNT(`id`) as `count` ,avg(`winning`) as winning,avg(`level`) as level FROM `winning` where `sn` = '$sn' and `is_winning` = 1 and `level` = $level";
        return $conn->readConn($querySql);
    }
    /**
     * 本期是否参与
     * return Array
     * */
    public static function getWinningNewCy($conn,$sn,$uid){
        $querySql="SELECT `number` FROM `winning` where `uid` = '$uid' and `sn` = '$sn' order by `id` desc limit 1";
        return $conn->readConn($querySql);
    }
    /**
     * 抽奖记录
     * return Array
     * */
    public static function getWinningFunds($conn,$winningExample){
        $querySql="SELECT `id`,`sn`,`number`,`time`,`is_prize`,`is_winning`,`winning` FROM `winning` where `uid` = $winningExample->uid";
        return $conn->readConn($querySql,true);
    }
    /**
     * 参与抽奖
     * return Array
     * */
    public static function insetWinning($conn,$winningExample){
        $insertSql = "insert into `winning`(`uid`,`username`,`sn`,`number`)value('$winningExample->uid','$winningExample->username','$winningExample->sn','$winningExample->number')";
        return $conn->executeConn($insertSql);
    }
    /**
     * 编辑奖池信息
     * entry $WithdrawExample
     * return int
     * */
    public static function upWinningInside($conn,$Example){
        $joinSql="";
        if($Example->state !=null){
            $joinSql.=" `state` = '$Example->state',";
        }
        if($Example->end_time !=null){
            $joinSql.=" `end_time` = '$Example->end_time',";
        }
        if($Example->winning_num !=null){
            $joinSql.=" `winning_num` = '$Example->winning_num',";
        }
        if($Example->total !=null){
            $joinSql.=" `total` = '$Example->total',";
        }
        if($Example->av !=null){
            $joinSql.=" `av` = '$Example->av',";
        }
        if($Example->number !=null){
            $joinSql.=" `number` = '$Example->number',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `winning` SET $innerJoin WHERE `id` =$Example->id";
            return $conn->executeConn($upUserSql);
        }
    }
}